﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using MMS.DAO;
using log4net;

namespace MMS
{
    public partial class FormCustomerInfo : Form
    {
        private CustomerDTO customer = null;

        #region FormCustomerInfo
        
        public FormCustomerInfo()
        {
            InitializeComponent();
            this.Text = Utils.APP_TITLE;
            customer = new CustomerDTO();
            customer.Id = "";
            txtId.Enabled = false;
        }

        public FormCustomerInfo(CustomerDTO CustomerDTO)
        {
            InitializeComponent();
            this.Text = Utils.APP_TITLE;
            this.customer = CustomerDTO;
            bbiSave.Visibility = BarItemVisibility.Never;
        }

        private void FormCustomerInfo_Shown(object sender, EventArgs e)
        {
            populateCustomerInfo();
        }
        #endregion

        private void clearAll()
        {
            txtId.Text = "";
            txtName.Text = "";

            txtAddress.Text = "";
            cbActive.Checked = false;
            txtEmail.Text = "";
            txtWebsite.Text = "";
            txtPhone1.Text = "";
            txtPhone2.Text = "";
        }

        

        private void populateCustomerInfo()
        {
            try
            {
                txtId.Text = customer.Id;
                txtName.Text = customer.Name;

                txtAddress.Text = customer.Address;
                cbActive.Checked = customer.Active;
                txtEmail.Text = customer.Email;
                txtWebsite.Text = customer.Website;
                txtPhone1.Text = customer.Phone1;
                txtPhone2.Text = customer.Phone2;

                //picture.Image = customer.Picture;
            }
            catch (Exception e)
            {
                LogUtil.log.Error(e);
            }

        }

        private void saveCustomer()
        {
            // customer.Id=  txtId.Text ;
            customer.Name = txtName.Text;

            customer.Address = txtAddress.Text;
            customer.Active = cbActive.Checked;
            customer.Email = txtEmail.Text;
            customer.Website = txtWebsite.Text;
            customer.Phone1 = txtPhone1.Text;
            customer.Phone2 = txtPhone2.Text;
            try
            {
                CustomerDAO customerDAO = new CustomerDAO(Param.ConnectionString.Value);
                if (customer.Id == "")
                {
                    ValidateUtil.isValidCustomerDTO(customer);
                    customerDAO.Insert(customer);
                }
                else
                {
                    customerDAO.Update(customer);
                }
            }
            catch (System.Exception ex)
            {
                LogUtil.log.Error(ex);
            }
           
        }

        private void FormCustomerInfo_Load(object sender, EventArgs e)
        {

        }

        private void barButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string senderName = e.Item.Name;
            if (senderName.Equals(bbiSave.Name))
            {
                saveCustomer();
                clearAll();
            }
            else if (senderName.Equals(bbiSaveAndExit.Name))
            {
                saveCustomer();
                this.Close();
            }
            else if (senderName.Equals(bbiExit.Name))
            {
                this.Close();
            }
        }

       

    }
}
